<?php

namespace Model;

use Config\Cfg;
use Extension\Tools;
use Handler\ManagerHandler;

class User {
	/**
	 *
	 * @var int
	 */
	public $m_Id;
	/**
	 * 账号
	 *
	 * @var string
	 */
	public $m_Name;
	/**
	 * 原始密码
	 *
	 * @var string
	 */
	public $m_BasePass;
	/**
	 * 加密密码
	 *
	 * @var string
	 */
	public $m_EncPass;
	/**
	 * 昵称
	 *
	 * @var string
	 */
	public $m_Nick;
	/**
	 * 金币数
	 *
	 * @var int
	 */
	public $m_Coin;
	/**
	 * 性别
	 *
	 * @var int
	 */
	public $m_Sex;
	/**
	 * 星座
	 *
	 * @var unknown
	 */
	public $m_Con;
	/**
	 * 个性签名
	 *
	 * @var string
	 */
	public $m_Sign;
	/**
	 * 所在地
	 *
	 * @var unknown
	 */
	public $m_Loc;
	/**
	 * 电话
	 *
	 * @var unknown
	 */
	public $m_Phone;
	/**
	 * 微信
	 *
	 * @var unknown
	 */
	public $m_WeChat;
	/**
	 * QQ
	 *
	 * @var unknown
	 */
	public $m_QQ;
	/**
	 * 电子邮箱
	 *
	 * @var unknown
	 */
	public $m_Email;
	/**
	 * 经验值
	 *
	 * @var int
	 */
	public $m_Exp;
	/**
	 * 等级
	 *
	 * @var int
	 */
	public $m_Level;
	/**
	 * 微信openid
	 *
	 * @var string
	 */
	public $m_openId;
	/**
	 * 微信UnionID
	 *
	 * @var unknown
	 */
	public $m_UnionId;
	/**
	 * 头像
	 *
	 * @var string
	 */
	public $m_Head;
	/**
	 * token
	 *
	 * @var string
	 */
	public $m_Token;
	/**
	 * 注册时间
	 *
	 * @var string
	 */
	public $m_RegistDate;
	/**
	 * 代理
	 *
	 * @var unknown
	 */
	public $m_Agent;
	/**
	 * 状态
	 *
	 * @var int(0:正常，1：封停，2：禁止，3：特殊用户，4：测试用户)
	 */
	public $m_Status;
	/**
	 * GatewayWorker ClientId
	 *
	 * @var string
	 */
	public $m_ClientId;
	/**
	 * 当前状态
	 *
	 * @var unknown
	 */
	public $m_CurStatus;
	/**
	 * 心跳
	 *
	 * @var unknown
	 */
	public $m_HeartBeat;
	/**
	 * 当前游戏ID
	 *
	 * @var unknown
	 */
	public $m_GameId;
	/**
	 * 状态：在大厅
	 *
	 * @var integer
	 */
	const STATUS_LOBBY = 0;
	/**
	 * 状态：在游戏
	 *
	 * @var integer
	 */
	const STATUS_GAME = 1;
	/**
	 * 消息日志
	 *
	 * @var array
	 */
	private $m_DataLog;
	/**
	 * 登录日志
	 *
	 * @var array
	 */
	private $m_LoginLog;
	
	private $m_GameBefore;
	private $m_GameLater;
	/**
	 * 构造函数
	 */
	function __construct() {
	}
	/**
	 * 析构函数
	 */
	function __destruct() {
	}
	
	// ------------------------------------------------大厅部分------------------------------------------------//
	/**
	 * 登入
	 *
	 * @param unknown $clientId        	
	 * @param unknown $data        	
	 */
	public function mb_Login($clientId, $data) {
		$this->m_Id = $data ['id'];
		$this->m_Name = $data ['name'];
		$this->m_BasePass = $data ['basepass'];
		$this->m_EncPass = $data ['encpass'];
		$this->m_Coin = ( double ) $data ['coin'];
		$this->m_Sex = ( int ) $data ['sex'];
		$this->m_Con = $data ['con'];
		$this->m_Sign = $data ['sign'];
		$this->m_Loc = $data ['loc'];
		$this->m_Phone = $data ['phone'];
		$this->m_WeChat = $data ['wechat'];
		$this->m_QQ = $data ['qq'];
		$this->m_Email = $data ['email'];
		$this->m_Exp = ( int ) $data ['exp'];
		$this->m_Level = ( int ) $data ['level'];
		$this->m_Agent = $data ['agent'];
		
		$this->m_openId = $data ['openid'];
		$this->m_UnionId = $data ['unionid'];
		$this->m_Head = $data ['head'];
		$this->m_Token = $data ['token'];
		$this->m_RegistDate = $data ['registdate'];
		$this->m_Status = $data ['status'];
		
		$this->m_ClientId = $clientId;
		
		$this->m_HeartBeat = time ();
		$this->m_GameId = 0;
		
		// 初始化用户数据日志
		$this->m_DataLog = array ();
		// 初始化用户登录日志
		$this->m_LoginLog = array ();
		// 设置当前状态：在大厅
		$this->m_CurStatus = self::STATUS_LOBBY;
		// 记录登录日志
		$this->mb_SetLoginLog ( 'Lobby', 'Login' );
		
		$this->m_GameBefore = 0;
		$this->m_GameLater = 0;
	}
	/**
	 * 登出
	 *
	 * @return string
	 */
	public function mb_Logout() {
		$this->mb_SetLoginLog ( 'Lobby', 'Logout' );
		
		$data = array ();
		$data ['basepass'] = $this->m_BasePass;
		$data ['encpass'] = $this->m_EncPass;
		$data ['coin'] = $this->m_Coin;
		$data ['sex'] = $this->m_Sex;
		$data ['con'] = $this->m_Con;
		$data ['sign'] = $this->m_Sign;
		$data ['loc'] = $this->m_Loc;
		$data ['phone'] = $this->m_Phone;
		$data ['wechat'] = $this->m_WeChat;
		$data ['qq'] = $this->m_QQ;
		$data ['email'] = $this->m_Email;
		$data ['exp'] = $this->m_Exp;
		$data ['level'] = $this->m_Level;
		$data ['openid'] = $this->m_openId;
		$data ['head'] = $this->m_Head;
		$data ['token'] = $this->m_Token;
		$data ['status'] = $this->m_Status;
		return $data;
	}
	
	/**
	 * 保存用户数据
	 *
	 * @param int $coin
	 *        	金币
	 * @param string $remark
	 *        	备注
	 * @param string $channel
	 *        	渠道（0：大厅，other：游戏id）
	 * @return string|boolean
	 */
	public function mb_SaveData($coin, $remark = '', $channel = '0') {
		$newCoin = $this->m_Coin + $coin;
		if ($newCoin < 0) {
			return Cfg::COIN_NAME . '不足';
		}
		$this->m_Coin = $newCoin;
		$this->mb_SetDataLog ( $coin, $newCoin, $remark, $channel );
		return true;
	}
	/**
	 * 获取用户数据
	 *
	 * @return int[]
	 */
	public function mb_GetData() {
		return array (
				'coin' => $this->m_Coin 
		);
	}
	/**
	 * 保存用户信息
	 *
	 * @param int $sex        	
	 * @param string $sign        	
	 */
	public function mb_SaveInfo($sex, $con, $sign, $loc, $phone, $wechat, $qq, $email) {
		$this->m_Sex = $sex;
		$this->m_Con = $con;
		$this->m_Sign = $sign;
		$this->m_Loc = $loc;
		$this->m_Phone = $phone;
		$this->m_WeChat = $wechat;
		$this->m_QQ = $qq;
		$this->m_Email = $email;
	}
	/**
	 * 获取用户信息
	 *
	 * @return string[]
	 */
	public function mb_GetInfo() {
		return array (
				'sex' => $this->m_Sex,
				'con' => $this->m_Con,
				'sign' => $this->m_Sign,
				'loc' => $this->m_Loc,
				'phone' => $this->m_Phone,
				'wechat' => $this->m_WeChat,
				'qq' => $this->m_QQ,
				'email' => $this->m_Email 
		);
	}
	/**
	 * 获取用户所有数据信息
	 *
	 * @return number[]|string[]|\Model\unknown[]|\Model\int(0:正常，1：封停，2：禁止，3：特殊用户，4：测试用户)[]|NULL[]
	 */
	public function mb_GetAllData() {
		return array (
				'id' => $this->m_Id,
				'name' => $this->m_Name,
				'basepass' => $this->m_BasePass,
				'encpass' => $this->m_EncPass,
				'coin' => $this->m_Coin,
				'sex' => $this->m_Sex,
				'con' => $this->m_Con,
				'sign' => $this->m_Sign,
				'loc' => $this->m_Loc,
				'phone' => $this->m_Phone,
				'wechat' => $this->m_WeChat,
				'qq' => $this->m_QQ,
				'email' => $this->m_Email,
				'agent' => $this->m_Agent,
				
				'exp' => $this->m_Exp,
				'level' => $this->m_Level,
				'openid' => $this->m_openId,
				'unionid' => $this->m_UnionId,
				'head' => $this->m_Head,
				'token' => $this->m_Token,
				'status' => $this->m_Status 
		);
	}
	// ----------------------------------------用户数据日志---------------------------------------- //
	/**
	 * 设置数据日志
	 *
	 * @param unknown $addCoin
	 *        	金币变化量
	 * @param unknown $newCoin
	 *        	新的金币数量
	 * @param unknown $remark
	 *        	标注
	 * @param unknown $channel
	 *        	大厅（0） 或游戏ID
	 */
	private function mb_SetDataLog($addCoin, $newCoin, $remark, $channel) {
		$this->m_DataLog [] = array (
				'name' => $this->m_Name,
				'coin' => $addCoin,
				'curcoin' => $newCoin,
				'remark' => $remark,
				'channel' => $channel,
				'date' => Tools::mb_GetCurDateTime () 
		);
	}
	public function mb_GetDataLogs() {
		return $this->m_DataLog;
	}
	// ----------------------------------------用户登录日志---------------------------------------- //
	/**
	 * 设置登录日志
	 *
	 * @param unknown $scene        	
	 * @param unknown $type        	
	 */
	public function mb_SetLoginLog($scene, $type) {
		$this->m_LoginLog [] = array (
				'name' => $this->m_Name,
				'scene' => $scene,
				'type' => $type,
				'date' => Tools::mb_GetCurDateTime () 
		);
	}
	/**
	 * 获取登录日志
	 *
	 * @return array
	 */
	public function mb_GetLoginLog() {
		return $this->m_LoginLog;
	}
	// ----------------------------------------用户游戏---------------------------------------- //
	public function mb_EnterGame($gameId) {
		$this->m_CurStatus = self::STATUS_GAME;
		$this->m_GameId = $gameId;
		
		$this->m_GameBefore = $this->m_Coin;
	}
	public function mb_ExitGame() {
		$this->m_GameLater = $this->m_Coin;
		ManagerHandler::mb_GetInstance()->mb_InsertGameLog($this->m_Id, $this->m_Name, $this->m_GameBefore, 0, $this->m_GameLater, $this->m_GameId);
		
		$this->m_CurStatus = self::STATUS_LOBBY;
		$this->m_GameId = 0;
	}
	
	/**
	 * 获取私有成员
	 *
	 * @param unknown $property_name        	
	 * @return unknown|NULL
	 */
	public function __get($property_name) {
		if (isset ( $this->$property_name )) {
			return ($this->$property_name);
		} else {
			return (NULL);
		}
	}
}